package com.droidnova.android.games.vortex;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLSurfaceView.Renderer;

public class VortexRender implements Renderer {
	
	private final static String TAG = VortexRender.class.getSimpleName();
	private float green = 0.2f;
	private float red = 0.9f;
	private float blue = 0.2f;
	private ShortBuffer indexBuffer;
	private FloatBuffer vertexBuffer;
	private int nrOfVertices = 0;
	private float xAngle;
	private float yAngle;
	private FloatBuffer colorBuffer;
	private float width = 320f;
	private float height = 480f;

	@Override
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
//		gl.glLoadIdentity();
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
		
		for(int i=0;i<=10;i++){
			gl.glLoadIdentity();
			gl.glTranslatef(0.0f,-1f, -1.0f + -1.5f*i);
			gl.glRotatef(xAngle, 1f, 0f, 0f);
			gl.glRotatef(yAngle, 0f, 1f, 0f);
			gl.glDrawElements(GL10.GL_TRIANGLES, nrOfVertices,GL10.GL_UNSIGNED_SHORT, indexBuffer);
		}
		
//		gl.glColor4f(0.5f, 0f, 0f, 0.5f);

	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// TODO Auto-generated method stub
		this.width = width;
		this.height = height;
		gl.glViewport(0, 0, width, height);
	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// TODO Auto-generated method stub
		gl.glMatrixMode(GL10.GL_PROJECTION);
		float ratio = width/height;
		float size = .01f*(float)Math.tan(Math.toRadians(45.0)/2);
		gl.glFrustumf(-size,size,-size/ratio,size/ratio,0.01f,100.0f);
//		gl.glOrthof(-1f,1f,-1/ratio,1/ratio,0.01f,100.0f);
		
		gl.glViewport(0, 0, (int)width, (int)height);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glClearColor(0f,0f,0f,1.0f);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glFrontFace(GL10.GL_CCW);
		gl.glCullFace(GL10.GL_BACK);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		initTriangle();
	}
	
	public void setColor(float r,float g ,float b){
		this.green = g;
		this.red = r;
		this.blue = b;
	}

	private void initTriangle(){
		float[] coords = {
				-0.5f,-0.5f,0.5f,
				0.5f,-0.5f,0.5f,
				0f,-0.5f,-0.5f,
				0f,0.5f,0f
		};
		float[] colors = {
				1.0f,0f,0f,1.0f,
				0f,1f,0f,1f,
				0f,0f,1f,1f,
				1f,1f,1f,1f
		};
		short[] indices = new short[]{
				0,1,3,
				0,2,1,
				0,3,2,
				1,2,3
		};
		
		ByteBuffer vbb = ByteBuffer.allocateDirect(coords.length*4);
		vbb.order(ByteOrder.nativeOrder());
		vertexBuffer = vbb.asFloatBuffer();
		
		nrOfVertices = coords.length;
		
		ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length*2);
		ibb.order(ByteOrder.nativeOrder());
		indexBuffer = ibb.asShortBuffer();
		
		ByteBuffer cbb = ByteBuffer.allocateDirect(colors.length*4);
		cbb.order(ByteOrder.nativeOrder());
		colorBuffer = cbb.asFloatBuffer();
		
		
		
		vertexBuffer.put(coords);
		indexBuffer.put(indices);
		colorBuffer.put(colors);
		
		vertexBuffer.position(0);
		indexBuffer.position(0);
		colorBuffer.position(0);
	}
	
	
	public void setXAngle(float xAngle){
		this.xAngle = xAngle;
	}
	
	public void setYAngle(float yAngle){
		this.yAngle = yAngle;
	}	
	
	public float getXAngle(){
		return this.xAngle;
	}
	
	public float getYAngle(){
		return this.yAngle;
	}	
}
